#!/usr/bin/env python

#Give this the output filename from pyphon_twotier -- it'll give you ERC-set pairs which together define an Input-intermediate-output relationship consistent with the provided observations.

import RNF
from permutationdistance import getrankings, set_dif
	
def print_languages(languages, log, i):
	print
	for language in languages:
		print "Language", i
		log.write("Language "+str(i)+"\n")
		print "Cyclic ERCs", language[0]
		log.write(str(language[0]))
		log.write("\n")
		rs=getrankings(language[0], 10)
		print "Sample Cyclic Rankings", rs
		log.write(str(language[1]))
		log.write("\n")
		print "Postlex ERCs", language[1]
		rs=getrankings(language[1], 10)
		print "Sample Postlex Rankings", rs

		for member in language[2]:
			print member[0], member[1], member[2]
			log.write(str(member[0])+" "+str(member[1])+" "+str(member[2])+"\n")
		#print language[3]
			
def compatible_languages(cohort, languages):
	retlanguages = []
	if languages == []: 
		for member in cohort:
			retlanguages.append([member[3].rsplit("_"), member[4].rsplit("_"), [member]])

	else:
		for language in languages:
			for member in cohort:
				es1 = RNF.RNF(language[0]+member[3].rsplit("_"))
				es2 = RNF.RNF(language[1]+member[4].rsplit("_"))
				
				#are the above really equivalent to concatenate + simplify?
				
				es1 = language[0]+member[3].rsplit("_")
				
				es2 = language[1]+member[4].rsplit("_")
				
				for erc in es1:
					if not "l" in erc:
						es1.remove(erc)
	
				for erc in es2:
					if not "l" in erc:
						es2.remove(erc)
				
	
				if (RNF.consistent(es1) and RNF.consistent(es2)):
					#print "success"
					retlanguages.append([es1, es2, language[2]+[member]])
				#else:
					#print "fail"
					#print RNF.consistent(es1), es1
					#print RNF.consistent(es2), es2
					
					
	todelete = []				
	for i in range(len(retlanguages)):
		for j in range(i):
			#print "checking", i, j
			if retlanguages[i][0] == retlanguages[j][0] and retlanguages[i][1] == retlanguages[j][1]:
				todelete.append(i)
	pfretlanguages=[]
	for i in range(len(retlanguages)):
		if i not in todelete:
			pfretlanguages.append(retlanguages[i])
	
	return pfretlanguages

def find2tlangs(infilename):
	if ".csv" in infilename:
		infilename = infilename[:-4]
		
	log = open(infilename+"_languages.csv",'w')
	
		
	infile = open(infilename + ".csv", 'r')
	
	
	
	seqs = []
	for line in infile:
		if ("input form" in line):
			continue
		line = line[:-1]
		seq = line.rsplit(",") #build pairs of IO mappings
		seqs.append(seq)
	
	cohorts = []
	cohort = []
	#def next_input(erc, combos):
		
	seqs.sort()
		
	prev = ""
	for i in range(len(seqs)):
		if prev == "" or seqs[i][0] == prev:
			cohort.append(seqs[i])
		else:
			cohorts.append(cohort)
			cohort=[seqs[i]]
		prev = seqs[i][0]
	cohorts.append(cohort)
	
	
		#next_input()

		
		
		
	languages = []
	
	for cohort in cohorts:
		languages = compatible_languages(cohort, languages)
		print "Adding cohort with", len(languages), "languages:", cohort[0][0]
		#print_languages(languages)
		if len(languages)==0:
			print "No languages found. Sorry!"
			break
			
	annotated_languages = []
	i=0
	for language in languages:
		i+=1
		#language.append(cover_dist(language[0], language[1]))
		annotated_languages.append(language)
		print_languages([language], log, i)
	